<?php
/*
 * 行末注释一般为功能注释，代码注释一般为测试请勿删除
 * 
 * */
class commonMod
{
	public $tpl;
	public $model;
	public function __construct()
	{	
		//header("Content-type: text/html; charset=utf-8");	
		global $model,$tpl;
		
		$this->model=$model;
		$this->tpl=$tpl;
		$this->checkLogin();//用户登录验证
	}
	//模板变量解析
	public function assign($name, $value)
	{
		return $this->tpl->assign($name, $value);
	}
	//模板输出
	public function display($tpl='')
	{	
		return $this->tpl->display($tpl);	
	}
		
	//直接跳转
	public  function redirect($url)
	{
		header('location:'.$url,false,301);
		exit;
	}
	//操作成功之后跳转,默认三秒钟跳转
	public  function success($msg,$url=NULL,$waitSecond=3)
	{
		if($url==NULL)
			$url=__URL__;
		$this->assign('message',$msg);
		$this->assign('url',$url);
		$this->assign('waitSecond',$waitSecond);
		$this->display('success');
		exit;
	}
	//出错之后跳转，后退到前一页
	public  function error($msg)
	{
		header("Content-type: text/html; charset=utf-8"); 
		$msg="alert('$msg');";
		echo "<script>$msg history.go(-1);</script>";
		exit;
	}

	//检查是否登录,如果还没有登录，且当前操作不是登录或者验证码生成操作，则跳到登录页面
	public  function checkLogin()
	{
		if(empty($_SESSION['uid'])||empty($_SESSION['username'])||empty($_SESSION['groupid']))
		{
			//如果当前操作是登录或者验证码生成，则放行
			if($_GET['_module']=='index'&&in_array($_GET['_action'],array('login')))
			{
				return false;
			}
			//跳转到登录页面
			$this->redirect(__APP__.'/index/login.html');
		}
		return true;
	}
	//分页 $url:基准网址，$totalRows: $listRows列表每页显示行数$rollPage 分页栏每页显示的页数
	public function  page($url,$totalRows,$listRows,$rollPage,$model)
	{
		require_once(CP_PATH.'lib/Page.class.php');
		$page=new page();
		return $page->show($url,$totalRows,$listRows,$rollPage,$model);
	}
	//文件上传
	protected function _upload($upload_dir)
    {
		require_once(CP_PATH.'lib/UploadFile.class.php');
		$upload = new UploadFile();
		//设置上传文件大小
		$upload->maxSize=1024*1024*2;//最大2M
		//设置上传文件类型
		$upload->allowExts  = explode(',','jpg,gif,png,bmp');
	
		//设置附件上传目录
		$upload->savePath ='./upload/'.$upload_dir."/";
		$upload->saveRule = cp_uniqid;

		if(!$upload->upload())
		{
			//捕获上传异常
			$this->error($upload->getErrorMsg());
		}
		else 
		{
			//取得成功上传的文件信息
			return $upload->getUploadFileInfo();
		}
	}
	
	//判断用户唯一性
	protected function checkUniqu($condition,$table){
		$result = $this->model->table($table)->where($condition)->count();
		if($result){
			return TRUE;
		}else{
			return FALSE;
		}
	}
	
	//无限极分类
	protected function getCat($id=0){
		require(CP_PATH.'lib/Category.class.php'); // 导入Category.class.php无限分类
		//查询分类信息
		$data=$this->model->field('id,pid,name')->table('category')->select();         
		$cat = new Category(array('id','pid','name','cname'));		
		
		return $cat->getTree($data,$id);//获取分类数据树结构
	}

	//无限极分类获取上级
	protected function getRightCat($id){
		$pos        = array('id'=>$id);
		$position   = $this->model->table('category')->where($pos)->find();
		$dep        = array('id'=>$position['pid']);
		$department = $this->model->table('category') ->where($dep)->find();
		$cat['parent'] = $department['name'];
		$cat['child']  = $position['name'];
		return $cat;
	}

}
?>